package com.internetCafes.spms.web.sys.service.impl;

import com.google.code.kaptcha.Producer;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.generic.GenericDao;
import com.internetCafes.spms.core.generic.GenericServiceImpl;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.core.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.internetCafes.spms.web.sys.dao.CaptchaInfoMapper;
import com.internetCafes.spms.web.sys.model.CaptchaInfo;
import com.internetCafes.spms.web.sys.service.CaptchaInfoService;


/**
 * 系统验证码 CaptchaInfoService 实现类
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2019-10-28 10:21:45
 */
@Service("captchaInfoService")
public class CaptchaInfoServiceImpl extends GenericServiceImpl<CaptchaInfo, String> implements CaptchaInfoService {

    @Autowired
    private CaptchaInfoMapper captchaInfoMapper;

    @Autowired
    private Producer producer;

    @Override
    public BufferedImage getCaptcha(String uuid) {
        if(StringUtils.isBlank(uuid)){
            throw new RRException("uuid不能为空");
        }
        //生成文字验证码
        String code = producer.createText();

        CaptchaInfo captchaEntity = new CaptchaInfo();
        captchaEntity.setUuid(uuid);
        captchaEntity.setCode(code);
        //5分钟后过期
        captchaEntity.setExpireTime(DateUtils.getBeAfMinute(new Date(), 5));
        this.save(captchaEntity);

        return producer.createImage(code);
    }

    @Override
    public boolean validate(String uuid, String code) {


        CaptchaInfo captchaEntity = captchaInfoMapper.selectByPrimaryKey(uuid);
        if(captchaEntity == null){
            return false;
        }

        //删除验证码
        captchaInfoMapper.deleteByPrimaryKey(uuid);

        if(captchaEntity.getCode().equalsIgnoreCase(code) && captchaEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
            return true;
        }

        return false;
    }

    @Override
    public List<CaptchaInfo> query(Map<String, Object> params) {
        return captchaInfoMapper.selectByParams(params);
    }

    @Override
    public CaptchaInfo findById(String id) {
        return captchaInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CaptchaInfo> queryByIds(List<String> ids) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);
        return captchaInfoMapper.selectByParams(params);
    }

    @Override
    public List<CaptchaInfo> page(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return captchaInfoMapper.selectByParams(params);
    }

    @Override
    public long count(Map<String, Object> params) {
        return captchaInfoMapper.countByParams(params);
    }

    @Override
    public CaptchaInfo findFirst(Map<String, Object> params) {
        List<CaptchaInfo> list = captchaInfoMapper.selectByParams(params);
        CaptchaInfo captchaInfo =null;
        if (list != null && list.size() > 0) {
            captchaInfo =list.get(0);
        }
        return captchaInfo;
    }

    @Override
    public int save(CaptchaInfo captchaInfo) {
        return captchaInfoMapper.insert(captchaInfo);
    }

    @Override
    public int saveBatch(List<CaptchaInfo> captchaInfoList) {
        return captchaInfoMapper.insertBatch(captchaInfoList);
    }

    @Override
    public int saveSelective(CaptchaInfo captchaInfo) {
        return captchaInfoMapper.insertSelective(captchaInfo);
    }

    @Override
    public int update(CaptchaInfo captchaInfo) {
        return captchaInfoMapper.updateByPrimaryKey(captchaInfo);
    }

    @Override
    public int updateSelective(CaptchaInfo captchaInfo) {
        return captchaInfoMapper.updateByPrimaryKeySelective(captchaInfo);
    }

    @Override
    public int delete(String id) {
        return captchaInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(List<String> ids) {
        return captchaInfoMapper.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public void deleteBatch(String[] ids) {
        if(ids != null && ids.length > 0){
            for(String id : ids){
                delete(id);
            }
        }
    }

    @Override
    public GenericDao<CaptchaInfo, String> getDao() {
        return captchaInfoMapper;
    }

    /**
	 * DataGrid
	 */
    @Override
    public DataGrid dataGrid(Map<String, Object> params, int pageNo, int pageSize) {

        List<CaptchaInfo> captchaInfoList = page(params, (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize, pageSize);
        long count = count(params);

        DataGrid dg = new DataGrid(captchaInfoList, count, pageSize, pageNo);
        return dg;
    }
}